package com.migu.contacts;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.viewpager2.widget.ViewPager2;

import com.frank.libandroid.BaseActivity;
import com.frank.libandroid.BaseAdapter;
import com.migu.contacts.databinding.ActivityGuideBinding;
import com.migu.contacts.databinding.ItemSplashBinding;
import com.migu.contacts.util.UserCenter;
import com.tencent.mmkv.MMKV;
import com.zhpan.indicator.IndicatorView;
import com.zhpan.indicator.utils.IndicatorUtils;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends BaseActivity<ActivityGuideBinding> {
    public static final String Key = "isFirst";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        boolean isFirst = MMKV.defaultMMKV().getBoolean(Key, true);
        if (!isFirst) {
            start(SplashActivity.class);
            finish();
        }
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        super.initView();

        BaseAdapter<ItemSplashBinding, GuideBean> adapter = new BaseAdapter<ItemSplashBinding, GuideBean>() {
        };
        mBinding.viewPager.setAdapter(adapter);
        List<GuideBean> list = new ArrayList<>();
        list.add(new GuideBean(R.drawable.guide1, "轻松管理人脉", "一站式管理联系人，让您的人脉圈一目了然"));
        list.add(new GuideBean(R.drawable.guide2, "事项管理与提醒", "掌握人脉动态，生日、节日、重要事项等一应俱全"));
        list.add(new GuideBean(R.drawable.guide3, "重要事项倒计时", "让您从容准备，不错过任何重要时刻"));
        adapter.setData(list);
        mBinding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                if (position == 2) {
                    mBinding.indicatorView.setVisibility(View.GONE);
                    mBinding.start.setVisibility(View.VISIBLE);
                } else {
                    mBinding.indicatorView.setVisibility(View.VISIBLE);
                    mBinding.start.setVisibility(View.GONE);
                }
            }
        });
        IndicatorView indicatorView = mBinding.indicatorView;
        indicatorView.setSliderGap(IndicatorUtils.dp2px(4f));
        indicatorView.setSliderHeight(IndicatorUtils.dp2px(8));
        indicatorView.setSliderWidth(IndicatorUtils.dp2px(8), IndicatorUtils.dp2px(20));
        indicatorView.setupWithViewPager(mBinding.viewPager);
    }

    public void go(View view) {
        MMKV.defaultMMKV().putBoolean(Key, false);
        start(UserCenter.getStarter());
        finish();
    }
}